package com.jsgt.greekfire.ui.wallet.export.fragment

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_backup_wallet_words.*
import kotlinx.android.synthetic.main.fragment_private_key.*
import org.web3j.utils.Numeric
import java.math.BigInteger

private const val ARG_PARAM1 = "param1"

class PrivateKeyFragment : BaseFragment() {
    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(requireActivity()).get(WalletViewModel::class.java)
    }
    private var param1: Int = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getInt(ARG_PARAM1)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_private_key
    }

    override fun main() {
        walletViewModel.getIdentity(param1)?.observe(this, Observer {
            txvKey.text = Numeric.toHexStringNoPrefix(BigInteger(it.getPriKeyByAES()))
        })
        txvCopy.setOnClickListener {
            //获取剪贴板管理器：
            val cm = requireActivity().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val mClipData = ClipData.newPlainText("Label", txvKey.text.toString())
            cm.setPrimaryClip(mClipData)
            ToastUtils.show(R.string.copy_success)
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Int) =
            PrivateKeyFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PARAM1, param1)
                }
            }
    }
}